export const HOOKS = [
    "beforeCreate",
    "created",
    "beforeMounted",
    "mounted",
    "beforeUpdate",
    "updated",
    "beforeDestroy",
    "destroyed"
]

// 策略模式
let starts = {}
starts.data = function (parentVal,childVal) {
    return childVal
}

starts.computed = function () {
}
starts.watch = function () {
}
starts.methods = function () {
}

// 遍历生命周期
HOOKS.forEach(hooks => {
    starts[hooks] = mergeHook
})

function mergeHook(parentVal, childVal) {
    if (childVal) {
        if (parentVal) {
            return parentVal.concat(childVal)
        } else {
            return [childVal]
        }
    } else {
        return parentVal
    }
}

export function mergeOptions(parent, child) { // {} child: 是mixin中的对象
    console.log(parent, child)

    // {created:{a,b,c},watch:[a,b]}
    const options = {}

    // 如果有父亲 没有儿子
    for (let key in parent) {
        mergeField(key)
    }

    // 有儿子 没有父亲
    for (let key in child) {
        mergeField(key)
    }

    function mergeField(key) {
        // 根据 key来操作
        if (starts[key]) {  // 策略模式
            options[key] = starts[key](parent[key], child[key])
        } else {
            options[key] = child[key]
        }
    }

    return options
}
